/**
 * 
 */
package cn.mxz.robots.province;

import java.util.Scanner;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.pojo.Student;

/**
 * 新疆
 * 排名:false
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-19
 */
public class Xinjiang extends Exam
{

	public static void main(String[] args)
	{
		Student student = new Student("15653301152534", "87932237", "130825199708030020", "234", "");
		Xinjiang gansu = new Xinjiang(student);
		System.out.println(gansu.getCaptcha());
		System.out.println("请输入验证码：");
		@SuppressWarnings("resource")
		String captcha = new Scanner(System.in).next();
		System.out.println(gansu.login(captcha));
	}

	public String getCaptcha()
	{
		delCookie();
		Request captchaRequest = new Request("http://222.82.250.215:81/CheckCode.do", mSha1);
		// captchaRequest.addHeader("Referer",
		// "http://222.82.250.215:81/webpub/bpf/cjcx/search.jsp?sea_id=10002");
		return curlFile(captchaRequest);
	}

	public Xinjiang(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://222.82.250.215:81/webpub/bpf/cjcx/search.jsp?sea_id=10002", mSha1);
		loginRequest.setCharSet("utf-8");
		loginRequest.addHeader("Referer", "http://222.82.250.215:81/webpub/bpf/cjcx/search.jsp?sea_id=10002");
		// loginRequest.addHeader("Host", "xxcx.hebeea.edu.cn");
		// loginRequest.addHeader("Origin", "http://xxcx.hebeea.edu.cn");
		loginRequest.addParams("act", "submit");
		loginRequest.addParams("Submit", "查　询");
		loginRequest.addParams("commond", "search");
		loginRequest.addParams("A", mStudent.getStudentId());
		loginRequest.addParams("J", mStudent.getStudentPwd());
		loginRequest.addParams("randomCode", captcha);
		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("查询内容"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml.replace("&nbsp", "")));
		}
		else if (loginHtml.contains("没有与检索条件相符的信息"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else if (loginHtml.contains("验证码"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			System.out.println(loginHtml);
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();

		Elements tables = loginDocument.select("table[bgcolor=#9DB9C8]");
		result.put("exam_id", mStudent.getStudentId());
		result.put("name", tables.get(1).select("tr").get(0).select("td").get(1).text().trim());
		result.put("ranking", "");
		result.put("province", "新疆维吾尔自治区");

		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", tables.get(2).select("tr").get(0).select("td").get(1).text());
		scoreObject.put("数学", tables.get(3).select("tr").get(0).select("td").get(1).text());
		scoreObject.put("外语", tables.get(5).select("tr").get(0).select("td").get(1).text());
		scoreObject.put("综合", tables.get(4).select("tr").get(0).select("td").get(1).text());
		scoreObject.put("汉语", tables.get(6).select("tr").get(0).select("td").get(1).text());
		scoreObject.put("照顾分", tables.get(7).select("tr").get(0).select("td").get(1).text());
		scoreObject.put("总分", tables.get(8).select("tr").get(0).select("td").get(1).text());
		scoreObject.put("外（汉语）听力成绩", tables.get(10).select("tr").get(0).select("td").get(1).text());

		result.put("score", scoreObject.toString());
		return result.toString();
	}
}
